﻿namespace MEF_Test.Bricks.MediaBrowser
{
    using System.Windows;
    using System.Windows.Controls.Primitives;
    using System.Windows.Input;

    public static class BrowserSelectedNodeChanged
    {        
        private static readonly DependencyProperty BrowserSelectedNodeChangedBehaviorProperty
            = DependencyProperty.RegisterAttached(
            "BrowserSelectedNodeChangedBehavior",
            typeof(BrowserSelectedNodeChangedBehavior),
            typeof(BrowserSelectedNodeChanged),
            null);

        public static readonly DependencyProperty CommandProperty
            = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(BrowserSelectedNodeChanged),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty
            = DependencyProperty.RegisterAttached(
           "CommandParameter",
           typeof(object),
           typeof(BrowserSelectedNodeChanged),
           new PropertyMetadata(OnSetCommandParameterCallback));

        public static ICommand GetCommand(Selector control)
        {
            return control.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(Selector control, ICommand command)
        {
            control.SetValue(CommandProperty, command);
        }

        public static void SetCommandParameter(Selector control, object parameter)
        {
            control.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(Selector control)
        {
            return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Selector control = dependencyObject as Selector;
            if (control != null)
            {
                BrowserSelectedNodeChangedBehavior behavior = GetOrCreateBehavior(control);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback
            (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            Selector control = dependencyObject as Selector;
            if (control != null)
            {
                BrowserSelectedNodeChangedBehavior behavior = GetOrCreateBehavior(control);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static BrowserSelectedNodeChangedBehavior GetOrCreateBehavior(Selector control)
        {
            BrowserSelectedNodeChangedBehavior behavior =
                control.GetValue(BrowserSelectedNodeChangedBehaviorProperty) as BrowserSelectedNodeChangedBehavior;
            if (behavior == null)
            {
                behavior = new BrowserSelectedNodeChangedBehavior(control);
                control.SetValue(BrowserSelectedNodeChangedBehaviorProperty, behavior);
            }
            return behavior;
        }
    }
}